package com.ruoyi.sale.service.impl;

import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.sale.domain.SaleReturnProduct;
import com.ruoyi.sale.mapper.SaleReturnMapper;
import com.ruoyi.sale.domain.SaleReturn;
import com.ruoyi.sale.service.ISaleReturnService;

/**
 * 销售退货Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-11-04
 */
@Service
public class SaleReturnServiceImpl implements ISaleReturnService 
{
    @Autowired
    private SaleReturnMapper saleReturnMapper;

    /**
     * 查询销售退货
     * 
     * @param id 销售退货主键
     * @return 销售退货
     */
    @Override
    public SaleReturn selectSaleReturnById(Long id)
    {
        return saleReturnMapper.selectSaleReturnById(id);
    }

    /**
     * 查询销售退货列表
     * 
     * @param saleReturn 销售退货
     * @return 销售退货
     */
    @Override
    public List<SaleReturn> selectSaleReturnList(SaleReturn saleReturn)
    {
        return saleReturnMapper.selectSaleReturnList(saleReturn);
    }

    /**
     * 新增销售退货
     * 
     * @param saleReturn 销售退货
     * @return 结果
     */
    @Transactional
    @Override
    public int insertSaleReturn(SaleReturn saleReturn)
    {
        saleReturn.setCreateTime(DateUtils.getNowDate());
        int rows = saleReturnMapper.insertSaleReturn(saleReturn);
        insertSaleReturnProduct(saleReturn);
        return rows;
    }

    /**
     * 修改销售退货
     * 
     * @param saleReturn 销售退货
     * @return 结果
     */
    @Transactional
    @Override
    public int updateSaleReturn(SaleReturn saleReturn)
    {
        saleReturn.setUpdateTime(DateUtils.getNowDate());
        saleReturnMapper.deleteSaleReturnProductByReturnId(saleReturn.getId());
        insertSaleReturnProduct(saleReturn);
        return saleReturnMapper.updateSaleReturn(saleReturn);
    }

    /**
     * 批量删除销售退货
     * 
     * @param ids 需要删除的销售退货主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteSaleReturnByIds(Long[] ids)
    {
        saleReturnMapper.deleteSaleReturnProductByReturnIds(ids);
        return saleReturnMapper.deleteSaleReturnByIds(ids);
    }

    /**
     * 删除销售退货信息
     * 
     * @param id 销售退货主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteSaleReturnById(Long id)
    {
        saleReturnMapper.deleteSaleReturnProductByReturnId(id);
        return saleReturnMapper.deleteSaleReturnById(id);
    }

    /**
     * 新增销售退货产品信息
     * 
     * @param saleReturn 销售退货对象
     */
    public void insertSaleReturnProduct(SaleReturn saleReturn)
    {
        List<SaleReturnProduct> saleReturnProductList = saleReturn.getSaleReturnProductList();
        Long id = saleReturn.getId();
        if (StringUtils.isNotNull(saleReturnProductList))
        {
            List<SaleReturnProduct> list = new ArrayList<SaleReturnProduct>();
            for (SaleReturnProduct saleReturnProduct : saleReturnProductList)
            {
                saleReturnProduct.setReturnId(id);
                list.add(saleReturnProduct);
            }
            if (list.size() > 0)
            {
                saleReturnMapper.batchSaleReturnProduct(list);
            }
        }
    }
}
